package com.qf.controller;


import com.google.gson.Gson;
import com.qf.annotations.RequestMapping;
import com.qf.pojo.WXPayResult;
import com.qf.service.OderService;
import com.qf.service.Impl.OderServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@RequestMapping("/payment")
public class PayResultController {
    private Gson gson = new Gson();

    private OderService oderService = new OderServiceImpl();

    @RequestMapping("/payWeixin")
    public String startPay(HttpServletRequest request, HttpServletResponse response) {
        String oid = request.getParameter("oid");
        String url = "http://localhost:8888/payment/process";
        String price = "1";//支付一分钱
        String body = "wyf";
        String realpath = "http://ceshi.qfjava.cn:81/payment/weixinpay?orderId=" + oid + "&price=" + price + "&body=" + body + "&url=" + url;
        return "redirect:" + realpath;
    }



    @RequestMapping("/process")
    public String processPayResult(HttpServletRequest request, HttpServletResponse response) {
        String result = request.getParameter("result");
        System.err.println("支付的结果:"+result);
        WXPayResult wxPayResult = gson.fromJson(result, WXPayResult.class);
        String out_trade_no = wxPayResult.getResult().getOut_trade_no();//我们的订单号
        String result_code = wxPayResult.getResult().getResult_code();
        if ("success".equalsIgnoreCase(result_code)) {
            //支付成功,更新订单的状态
            oderService.processPay(out_trade_no);
        }

        return "ok";
    }


}
